<?php
/**
 * User: Tegic
 * Date: 2018/6/13
 * Time: 09:47
 */

namespace app\workerman;

use GatewayWorker\Lib\Gateway;

class Events
{
	
	public static function onConnect($client_id)
	{
		// 向当前client_id发送数据
		Gateway::sendToClient($client_id, json_encode([
			'id'      => $client_id,
			'type'    => 'init',
			'message' => '登录成功'
		]));
	}
	
	public static function onMessage($client_id, $data)
	{
	}
	
	public static function onClose($client_id)
	{
		if (isset($_SESSION['fromid'])) {
			$isUidOnline = Gateway::isUidOnline($_SESSION['toid']);
			if (!$isUidOnline) {
				return;
			}
			Gateway::sendToUid($_SESSION['toid'], json_encode([
				'type'   => 'loginout',
				'fromid' => $_SESSION['fromid'],
				'toid'   => $_SESSION['toid'],
				'msg'    => '已下线'
			]));
		}
	}
}